<template>
  <div>
    <div class='top'>
      <p>登录</p>
    </div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field v-model="username" name="username" label="用户名" placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]" />
        <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]" />
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { getLogin } from '@/axios/getData.ts'
import { showToast } from 'vant';
import useRouter from '@/router';


let username = ref('')
let password = ref('')


const onSubmit = (values) => {
  showLoadingToast({
    message: '加载中...',
    forbidClick: true,
    loadingType: 'spinner',
    duration:900,
    onClose: async() => {
      let arr = await getLogin(values)
      if (arr.code == 200) {
        showToast({
          message: '登录成功',
          position: 'bottom',
        })
        localStorage.setItem('token',arr.token)
        localStorage.setItem('username',username.value)
        
        useRouter.push('/')
      }else{
        showToast({
          message: '登录失败，用户名或密码错误',
          position: 'bottom',
        });
      }
    }
  });
  console.log(values);
};

</script>

<style scoped>
.top {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 50px;
  background-color: #fff;
  border-bottom: 1px solid #ccc;
}

.top p {
  font-size: 20px;
  font-weight: 600;
  color: #eeda9d;
}
</style>